package com.hanxiaozhang.filter;

import com.hanxiaozhang.constant.Constant;
import com.hanxiaozhang.redis.util.RedisUtil;
import com.hanxiaozhang.result.ResultCode;
import com.hanxiaozhang.result.Result;
import com.hanxiaozhang.util.JsonUtil;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br>
 * 〈验证码过滤器〉
 *
 * @author hanxinghua
 * @create 2021/4/4
 * @since 1.0.0
 */
@Slf4j
@Component
public class CaptchaFilter extends ZuulFilter {

    @Autowired
    private RedisUtil redisBaseUtil;

    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {

        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest serverHttpRequest = currentContext.getRequest();
        String uri = serverHttpRequest.getRequestURI();
        if (uri.contains("/oauth/token")) {
            String method = serverHttpRequest.getMethod();
            // 处理跨域Post发送两次请求
            if (Constant.OPTIONS.equals(method)) {
                return null;
            }
            Map<String, String[]> parameterMap = serverHttpRequest.getParameterMap();
            String[] captchaKeys = null, captchaCodes = null;
            if (!parameterMap.isEmpty()
                    && (captchaKeys = parameterMap.get("captcha_key")) != null
                    && (captchaCodes = parameterMap.get("captcha_code")) != null) {
                String captchaKey = captchaKeys[0];
                String captchaCode = captchaCodes[0];
                log.info("Request Captcha Parameters: key: [{}] ,code: [{}]", captchaKey, captchaCode);
                String redisCaptchaCode = redisBaseUtil.get(captchaKey);
                String responseBody = null;
                if (redisCaptchaCode == null) {
                    responseBody = JsonUtil.beanToJson(Result.error(ResultCode.LOGIN_CAPTCHA_EXPIRE));
                } else if (!captchaCode.trim().equalsIgnoreCase(redisCaptchaCode)) {
                    responseBody = JsonUtil.beanToJson(Result.error(ResultCode.LOGIN_CAPTCHA_ERROR));
                }
                if (responseBody != null) {
                    currentContext.setSendZuulResponse(false);
                    currentContext.setResponseStatusCode(200);
                    currentContext.getResponse().setContentType(Constant.APP_JSON_UTF_8);
                    log.info("Response Parameters: \n [{}]", responseBody);
                    currentContext.setResponseBody(responseBody);
                }
            }
        }
        return null;
    }
}
